package com.qa.service.impl;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import com.qa.IDao.calendarMapper;
import com.qa.IDao.personalEventMapper;
import com.qa.pojo.Calendars;
import com.qa.pojo.Users;
import com.qa.pojo.calendar;
import com.qa.pojo.personalEvent;
import com.qa.service.IEventService;

@Service("eventService")
public class EventServiceImpl implements IEventService {

	@Resource
	private personalEventMapper personalEventDao;
	@Resource
	private calendarMapper calendarDao;

	@Override
	public List<personalEvent> getPersonalEventsById(int usersid) {
		// TODO Auto-generated method stub
		return this.personalEventDao.selectPersonalEventsById(usersid);
	}

	@Override
	public boolean insertMyEvents(int usersid, String title) {
		// TODO Auto-generated method stub
		return this.personalEventDao.insertMyEvents(usersid, title);
	}

	@Override
	public int deleteMyEvents(int usersid, String title) {
		// TODO Auto-generated method stub
		return this.personalEventDao.deleteMyEvents(usersid, title);
	}

	@Override
	public boolean addMyCalendar(int usersid, String title, int allDay,
			String date) {

		return this.calendarDao.addMyCalendar(usersid, title, allDay, date);
	}

	@Override
	public int selectThisCalendar(int usersid, String title, int allDay,
			String date) {
		return this.calendarDao
				.selectThisCalendar(usersid, title, allDay, date);
	}

	@Override
	public List<calendar> selectMyCalendar(int usersid) {
		// TODO Auto-generated method stub
		return this.calendarDao.selectMyCalendar(usersid);
	}

	@Override
	public int deleteMyCalendar(int usersid, int id) {
		// TODO Auto-generated method stub
		return this.calendarDao.deleteMyCalendar(usersid, id);
	}

	@Override
	public int updateMyCalendarForDrop(int usersid, int id, int dayDelta) {
		// TODO Auto-generated method stub
		return this.calendarDao.updateMyCalendarForDrop(usersid, id, dayDelta);
	}

	@Override
	public int updateMyCalendarForResize(int usersid, int id, int dayDelta) {
		// TODO Auto-generated method stub
		return this.calendarDao
				.updateMyCalendarForResize(usersid, id, dayDelta);
	}

	@Override
	public List<Users> getSubordinateById(int usersid) {
		// TODO Auto-generated method stub
		return this.calendarDao.selectSubordinateById(usersid);
	}

	@Override
	public List<String> selectCurrentProject() {
		// TODO Auto-generated method stub
		return this.calendarDao.selectCurrentProject();
	}

	@Override
	public boolean addMyCalendarForOne(int usersid, String date,
			String projectname, Double work_time, String work_content) {
		// TODO Auto-generated method stub
		return this.calendarDao.addMyCalendarForOne(usersid, date, projectname,
				work_time, work_content);
	}

	@Override
	public int selectThisCalendarForOne(int usersid, String date,
			String projectname, Double work_time, String work_content) {
		// TODO Auto-generated method stub
		return this.calendarDao.selectThisCalendarForOne(usersid, date,
				projectname, work_time, work_content);
	}

	@Override
	public List<calendar> selectCalendarForOne(int id) {
		// TODO Auto-generated method stub
		return this.calendarDao.selectCalendarForOne(id);
	}

	@Override
	public List<Calendars> exportpersonevent(int usersid, String beginmonth,
			String endmonth) {
		// TODO Auto-generated method stub
		return this.calendarDao
				.exportpersonevent(usersid, beginmonth, endmonth);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void exportDataAll(List<Calendars> eventlist, String templatePath,
			HttpServletResponse response) {
		// 获取文件输入流，创建工作簿
		FileInputStream fis;
		try {
			fis = new FileInputStream(new File(templatePath));
			HSSFWorkbook workBook = new HSSFWorkbook(fis);
			SimpleDateFormat myFmt = new SimpleDateFormat("yyyyMM");
			String timeString = myFmt.format(new Date());
			// 设置导出文件名，并编码
			String fileName = "月度工时统计.xls";
			response.reset();
			response.setContentType("application/octet-stream;charset=UTF-8");
			try {
				response.setHeader(
						"Content-Disposition",
						"attachment;Filename="
								+ new String(fileName.getBytes("GBK"),
										"ISO-8859-1"));
			} catch (Exception e) {
				e.printStackTrace();
			}
			// 创建输出流
			OutputStream outputStream = new BufferedOutputStream(
					response.getOutputStream());

			exportData(workBook, eventlist, 0);

			// 删除模板页
			workBook.removeSheetAt(0);
			// 将工作簿写入输出流
			workBook.write(outputStream);
			// 关闭资源流
			fis.close();
			outputStream.flush();
			outputStream.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * exportUserData:将数据写入工作簿. <br/>
	 * 
	 * @author qiyongkang
	 * @param workBook
	 * @param userList
	 * @since JDK 1.6
	 */
	private void exportData(HSSFWorkbook workBook, List<Calendars> cas,
			int sheetNum) {
		// 克隆模板sheet
		HSSFSheet singleSheet = workBook.cloneSheet(0);

		singleSheet.setActive(true);
		// 设置克隆的sheet名称
		workBook.setSheetName(sheetNum, "月工时汇总");
		// 获取cellStyle
		// 定义起始行，从第8行开始
		int singleRowIndex = 1;
		// 格式化時間
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		HSSFCellStyle style = workBook.createCellStyle();
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
		style.setWrapText(true);// 设置自动换行

		HSSFCellStyle style2 = style;
		style2.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.0"));

		HSSFCellStyle style1 = style;
		HSSFDataFormat format = workBook.createDataFormat();
		style1.setDataFormat(format.getFormat("yyyy年m月d日"));

		// 开始遍历
		for (Calendars ca : cas) {
			try {
				// 起始列索引
				int singleColIndex = 0;
				// 创建行
				HSSFRow singleRow = singleSheet.createRow(singleRowIndex++);
				// 创建列
				HSSFCell singleCell = null;
				// 项目组0
				singleCell = singleRow.createCell(0);
				if (ca.getProjectname() == null) {
					ca.setProjectname("");
				}
				singleCell.setCellType(HSSFCell.CELL_TYPE_STRING);
				singleCell.setCellValue(ca.getProjectname());
				singleCell.setCellStyle(style);

				// 部门,1
				singleCell = singleRow.createCell(1);
				singleCell.setCellType(HSSFCell.CELL_TYPE_STRING);
				singleCell.setCellValue(ca.getDeptname());
				singleCell.setCellStyle(style);

				// 姓名,2
				singleCell = singleRow.createCell(2);
				singleCell.setCellType(HSSFCell.CELL_TYPE_STRING);
				singleCell.setCellValue(ca.getName());
				singleCell.setCellStyle(style);
				// pname 3
				singleCell = singleRow.createCell(3);
				singleCell.setCellType(HSSFCell.CELL_TYPE_STRING);
				singleCell.setCellValue(ca.getPname());
				singleCell.setCellStyle(style);
				// content4
				singleCell = singleRow.createCell(4);
				singleCell.setCellType(HSSFCell.CELL_TYPE_STRING);
				singleCell.setCellValue(ca.getContent());
				singleCell.setCellStyle(style);

				// dt 6
				singleCell = singleRow.createCell(6);
				singleCell.setCellValue(ca.getDt());
				singleCell.setCellStyle(style1);
				// worktime 5
				HSSFCellStyle cellStyle = workBook.createCellStyle();
				cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.0"));
				cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
				cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
				cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
				cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
				cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
				singleCell = singleRow.createCell(5);
				singleCell.setCellValue(ca.getWorktime());
				singleCell.setCellStyle(cellStyle);
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
		}

		singleSheet.setForceFormulaRecalculation(true);
	}

}